﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformCommon;

namespace L9SubSet.WriteAddrPro
{
	public partial class Form_QuickEditAddr : Form
	{
		public int startLampAddr = 1;				// 首灯地址
		public int startLampNum = 1;				// 起始灯具号
		public int endLampNum = 1;					// 结束灯具号
		public string subMode = "selected";			// 应用到控制器
		public int portIndex = -1;					// 应用到端口
		public int[] channelArray = null;			// 灯具通道数数组

		private string section = "QuickEditAddr";	// 多语言文件配置节名称

		private string tip = "提示";					// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_QuickEditAddr(int startLampAddrQuick, int startLampNumQuick, int endLampNumQuick, int portIndexQuick, int[] channelArrayQuick)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			startLampAddr = startLampAddrQuick;
			startLampNum = startLampNumQuick;
			endLampNum = endLampNumQuick;
			portIndex = portIndexQuick;
			channelArray = new int[channelArrayQuick.Length];
			Array.Copy(channelArrayQuick, channelArray, channelArray.Length);

			initEnvironment();
		}

		// 初始化运行环境
		private void initEnvironment()
		{
			startLampAddrTextBox.Text = startLampAddr.ToString();
			startLampNumTextBox.Text = startLampNum.ToString();
			endLampNumTextBox.Text = endLampNum.ToString();

			subComboBox.Items.Add("当前控制器");
			subComboBox.Items.Add("已勾选控制器");
			subComboBox.Items.Add("所有控制器");
			subComboBox.SelectedIndex = 0;

			portComboBox.Items.Add("已勾选端口");
			portComboBox.Items.Add("所有端口");
			for (int i = 1; i <= 8; i++)
			{
				portComboBox.Items.Add(i.ToString());
			}
			portComboBox.SelectedIndex = 0;
			portIndex = 9;

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < channelArray.Length; i++)
			{
				if (i == 0)
				{
					sb.Append(channelArray[i]);
				}
				else
				{
					sb.Append(",");
					sb.Append(channelArray[i]);
				}
			}
			channelRichTextBox.Text = sb.ToString();

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			subComboBox.Items[0] = LanguageOper.ReadValue(section, "sub.Item.0");
			subComboBox.Items[1] = LanguageOper.ReadValue(section, "sub.Item.1");
			subComboBox.Items[2] = LanguageOper.ReadValue(section, "sub.Item.2");

			portComboBox.Items[0] = LanguageOper.ReadValue(section, "port.Item.0");
			portComboBox.Items[1] = LanguageOper.ReadValue(section, "port.Item.1");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "quickEditAddr.Text");
			firstLabel.Text = LanguageOper.ReadValue(section, "firstLabel.Text");
			startLampLabel.Text = LanguageOper.ReadValue(section, "startLampLabel.Text");
			endLampLabel.Text = LanguageOper.ReadValue(section, "endLampLabel.Text");
			applySubLabel.Text = LanguageOper.ReadValue(section, "applySubLabel.Text");
			aplyPortLabel.Text = LanguageOper.ReadValue(section, "aplyPortLabel.Text");
			channelTipLabel.Text = LanguageOper.ReadValue(section, "channelTipLabel.Text");
			okButton.Text = LanguageOper.ReadValue(section, "okButton.Text");
			cancelButton.Text = LanguageOper.ReadValue(section, "cancelButton.Text");
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				firstLabel.Location = new Point(52, 14);
				startLampLabel.Location = new Point(28, 46);
				endLampLabel.Location = new Point(28, 78);
				aplyPortLabel.Location = new Point(40, 141);
			}
			else
			{
				firstLabel.Location = new Point(42, 14);
				startLampLabel.Location = new Point(6, 46);
				endLampLabel.Location = new Point(17, 78);
				aplyPortLabel.Location = new Point(23, 141);
			}
		}

		// 应用到控制器
		private void subComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (subComboBox.SelectedIndex == 0)
			{
				subMode = "current";
			}
			else if (subComboBox.SelectedIndex == 1)
			{
				subMode = "selected";
			}
			else if(subComboBox.SelectedIndex == 2)
			{
				subMode = "all";
			}
		}

		// 应用到端口
		private void portComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			portIndex = portComboBox.SelectedIndex;
		}

		// 检查输入参数
		private bool checkParameter()
		{
			bool errorFag = false;

			// 首灯地址
			int tmpStartLampAddr = -1;
			bool res = int.TryParse(startLampAddrTextBox.Text.Trim(), out tmpStartLampAddr);
			if (res == false || tmpStartLampAddr < 1)
			{
				MessageBoxEx.Show(this, "首灯地址输入错误：应为正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
				errorFag = true;
			}
			else
			{
				startLampAddr = tmpStartLampAddr;
			}

			// 起始灯具号
			int tmpStartLampNum = -1;
			res = int.TryParse(startLampNumTextBox.Text.Trim(), out tmpStartLampNum);
			if (res == false || tmpStartLampNum < 1)
			{
				MessageBoxEx.Show(this, "起始灯具号输入错误：应为正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
				errorFag = true;
			}
			else
			{
				startLampNum = tmpStartLampNum;
			}

			// 结束灯具号
			int tmpEndLampNum = -1;
			res = int.TryParse(endLampNumTextBox.Text.Trim(), out tmpEndLampNum);
			if (res == false || tmpEndLampNum < 1)
			{
				MessageBoxEx.Show(this, "结束灯具号输入错误：应为正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
				errorFag = true;
			}
			else
			{
				endLampNum = tmpEndLampNum;
			}

			// 保证起始灯具不大于结束灯具
			if (startLampNum > endLampNum)
			{
				int tmp = startLampNum;
				startLampNum = endLampNum;
				endLampNum = tmp;
			}

			// 应用到端口
			if (portComboBox.SelectedIndex == 0)
			{
				// 当前选择的端口
				portIndex = 9;
			}
			else if (portComboBox.SelectedIndex == 1)
			{
				// 所有端口
				portIndex = 0;
			}
			else
			{
				// 特定端口
				portIndex = portComboBox.SelectedIndex - 1;
			}

			// 灯具通道数
			string channelArrayStr = channelRichTextBox.Text.Trim().Replace("\n", "").Replace(" ", "").Replace("\t", "");
			if (channelArrayStr.Equals(""))
			{
				channelArray = new int[1] { 3 };
			}
			else
			{
				List<int> tmpList = new List<int>();
				string[] subStr = channelArrayStr.Split(',');
				foreach (string s in subStr)
				{
					if (s.Equals(""))
					{
						continue;
					}

					int tmpValue = -1;
					res = int.TryParse(s, out tmpValue);
					if (tmpValue > 0)
					{
						tmpList.Add(tmpValue);
					}
					else
					{
						MessageBoxEx.Show(this, "灯具通道数输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
						errorFag = true;
						break;
					}
				}
				if (errorFag == false)
				{
					channelArray = tmpList.ToArray();
					if (channelArray.Length == 0)
					{
						channelArray = new int[1] { 3 };
					}
				}
			}

			return !errorFag;
		}

		// 确定
		private void okButton_Click(object sender, EventArgs e)
		{
			if (checkParameter() == false)
			{
				return;
			}

			this.DialogResult = System.Windows.Forms.DialogResult.OK;
		}

		// 取消
		private void cancelButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}
	}
}
